import sys
from PyQt6.QtWidgets import QApplication, QWidget
from PyQt6.QtCore import Qt, QTimer
from again.Welcome.loading_screen_new import Ui_Form  # 导入生成的UI类


class MainWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle('Main Window')
        self.setGeometry(100, 100, 800, 600)

class SplashScreen(QWidget):
    def __init__(self):
        super(SplashScreen, self).__init__()

        # 初始化UI
        self.resize(730, 496)
        screen_geometry = QApplication.primaryScreen().availableGeometry()
        pos = screen_geometry.center()
        self.move(pos.x() - self.width() // 2, pos.y() - self.height() // 2)

        self.ui = Ui_Form()
        self.ui.setupUi(self)

        # 设置窗口标志来隐藏标题栏和边框
        self.setWindowFlags(Qt.WindowType.FramelessWindowHint)
        self.setAttribute(Qt.WidgetAttribute.WA_TranslucentBackground)

        # 确保布局没有边距和间距
        self.setContentsMargins(0, 0, 0, 0)
        self.ui.verticalLayout.setContentsMargins(0, 0, 0, 0)
        self.ui.verticalLayout.setSpacing(0)

        self.processValue = 0
        self.ui.labelInitialiazing.setText(f"正在初始化界面...\nInitializing...(0%)")

        # 定时器
        self.timer = QTimer(self)
        self.timer.timeout.connect(self.updateInfo)
        self.timer.start(50)  # 每 50 毫秒更新一次

    def updateInfo(self):
        if self.processValue < 100:
            self.processValue += 2  # 每次增加2%，这样总共更新50次，每次50毫秒，总时间2500毫秒
            self.ui.labelInitialiazing.setText(f"正在初始化界面...\nInitializing...({self.processValue}%)")
            QApplication.processEvents()  # 允许主进程处理事件
        else:
            self.timer.stop()
            self.showMainWindow()

    def showMainWindow(self):
        self.mainWin = MainWindow()
        self.mainWin.show()
        self.close()

if __name__ == '__main__':
    app = QApplication(sys.argv)

    # 创建启动画面
    splash = SplashScreen()
    splash.show()

    sys.exit(app.exec())
